package com.beagle.njddapp.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.oil.RateBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.event.UserInfoUpdateEvent;
import com.beagle.njddapp.ui.activity.dirver.DirverOrderActivity;
import com.beagle.njddapp.ui.activity.oil.OilDeviceManagerActivity;
import com.beagle.njddapp.ui.activity.oil.OilOwnerOrderActivity;
import com.beagle.njddapp.ui.activity.owner.DeviceManagerActivity;
import com.beagle.njddapp.ui.activity.owner.FindDirverActivity;
import com.beagle.njddapp.ui.activity.owner.OrderActivity;
import com.beagle.njddapp.ui.presenter.UserCenterPresenter;
import com.beagle.njddapp.ui.view.UserCenterView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

import static com.beagle.njddapp.core.UserService.ROLE_DIRVER;
import static com.beagle.njddapp.core.UserService.ROLE_OIL;
import static com.beagle.njddapp.core.UserService.ROLE_OWNER;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 个人中心
 */
public class UserCenterActivity extends BaseMvpActivity<UserCenterView, UserCenterPresenter> implements UserCenterView {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.iv_role)
    ImageView ivRole;
    @BindView(R.id.rl_count)
    RelativeLayout rlCount;
    @BindView(R.id.rl_find_dirver)
    RelativeLayout rlFind;
    @BindView(R.id.rl_work)
    RelativeLayout rlWork;
    @BindView(R.id.tv_work_title)
    TextView tvWorkTitle;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.rl_pj)
    RelativeLayout rlPj;
    @BindView(R.id.tv_pj)
    TextView tvPj;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.iv4)
    ImageView iv4;
    @BindView(R.id.iv5)
    ImageView iv5;
    private int role;


    @Override
    public int getLayoutId() {
        return R.layout.activity_usercenter;
    }



    @Override
    public void init() {
        EventBus.getDefault().register(this);
        StatusBarUtil.immersive(this);
        tvName.setText(UserService.getInstance().getUserInfoBean().getUserName());
        role=UserService.getInstance().getRole();
        if (role==ROLE_OWNER){
            ivRole.setImageResource(R.mipmap.profile_nameplateone);
        }else  if (role==ROLE_OIL){
            ivRole.setImageResource(R.mipmap.profile_nameplatethree);
            rlCount.setVisibility(View.GONE);
            rlFind.setVisibility(View.GONE);
            rlWork.setVisibility(View.VISIBLE);
            tvWorkTitle.setText("加油车管理");
            tvOrderTitle.setText("加油订单管理");
            presenter.getPj();
        }else  if (role==ROLE_DIRVER){
            ivRole.setImageResource(R.mipmap.profile_nameplatetwo);
            rlCount.setVisibility(View.GONE);
            rlFind.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_set,R.id.rl_back,R.id.rl_count,R.id.rl_wallet,R.id.rl_work,R.id.rl_find_dirver,R.id.rl_order,
            R.id.iv_head,R.id.tv_name,R.id.rl_invate})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_set:
                startActivity(new Intent(this,SettingActivity.class));
                break;
            case R.id.rl_back:
                onBackPressed();
                break;
            case R.id.rl_count:
                startActivity(new Intent(this, DeviceManagerActivity.class));
                break;
            case R.id.rl_wallet:
                ToastUtils.showShort("此功能即将开放");
//                startActivity(new Intent(this,WalletActivity.class));
                break;
            case R.id.rl_work:
                if (role==ROLE_OIL){
                    //加油车管理
                    startActivity(new Intent(this, OilDeviceManagerActivity.class));
                    return;
                }
                ToastUtils.showShort("此功能即将开放");
                break;
            case R.id.rl_find_dirver:
                startActivity(new Intent(this, FindDirverActivity.class));
                break;
            case R.id.rl_order:
                if (role==ROLE_OIL){
                    //加油车订单管理
                    startActivity(new Intent(this, OilOwnerOrderActivity.class));
                    return;
                }
                if (role==ROLE_DIRVER){
                    //驾驶员订单管理
                    startActivity(new Intent(this, DirverOrderActivity.class));
                    return;
                }
                startActivity(new Intent(this, OrderActivity.class));
                break;
            case R.id.iv_head :
            case R.id.tv_name :
                startActivity(new Intent(this,UserInfoActivity.class));
                break;
            case R.id.rl_invate:
                startActivity(new Intent(this,InvateActivity.class));
                break;
        }
    }

    @Override
    public UserCenterView getMvpView() {
        return this;
    }

    @Override
    public UserCenterPresenter createPresenter() {
        return new UserCenterPresenter();
    }

    @Override
    public void onError(String message) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void onEvent(UserInfoUpdateEvent userInfoUpdateEvent){
        tvName.setText(UserService.getInstance().getUserInfoBean().getUserName());

    }

    @Override
    public void showRate(RateBean result) {
//        rlPj.setVisibility(View.VISIBLE);
//
//        if (result.getRate()>=5){
//
//        }else if (result.getRate()>=4){
//            iv5.setImageResource(R.mipmap.score_star_nor);
//        }else if (result.getRate()>=3){
//            iv5.setImageResource(R.mipmap.score_star_nor);
//            iv4.setImageResource(R.mipmap.score_star_nor);
//        } else if (result.getRate()>=2){
//            iv5.setImageResource(R.mipmap.score_star_nor);
//            iv4.setImageResource(R.mipmap.score_star_nor);
//            iv3.setImageResource(R.mipmap.score_star_nor);
//        }
//        else if (result.getRate()>=1){
//            iv5.setImageResource(R.mipmap.score_star_nor);
//            iv4.setImageResource(R.mipmap.score_star_nor);
//            iv3.setImageResource(R.mipmap.score_star_nor);
//            iv2.setImageResource(R.mipmap.score_star_nor);
//        }
    }
}
